package org.summer.lawyer.handle;

import cn.hutool.core.date.DateUtil;
import jakarta.annotation.Resource;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.summer.common.core.utils.MapstructUtils;
import org.summer.common.satoken.utils.LoginHelper;
import org.summer.lawyer.constant.LawFirmScopeConstants;
import org.summer.lawyer.domain.BrokerageUser;
import org.summer.lawyer.domain.LawyerInfo;
import org.summer.lawyer.domain.bo.brokerageuser.BrokerageUserBo;
import org.summer.lawyer.domain.bo.lawyerinfo.LawyerInfoBo;
import org.summer.lawyer.domain.vo.brokerageuser.BrokerageUserVo;
import org.summer.lawyer.domain.vo.lawfirm.LawFirmVo;
import org.summer.lawyer.domain.vo.lawyerinfo.LawyerInfoVo;
import org.summer.lawyer.domain.vo.wxuser.WxUserVo;
import org.summer.lawyer.event.LawFirmChangeEvent;
import org.summer.lawyer.service.IBrokerageUserService;
import org.summer.lawyer.service.ILawyerInfoService;
import org.summer.lawyer.util.IdWorkerUtil;

/**
 *
 */
@Slf4j
@Getter
@Component
public class LawFirmChangeHandle implements ApplicationListener<LawFirmChangeEvent> {

    private LawFirmVo lawFirmVo;
    private WxUserVo wxUserVo;

    @Resource
    private ILawyerInfoService lawyerInfoService;
    @Resource
    private IBrokerageUserService brokerageUserService;


    @Async
    @Override
    public void onApplicationEvent(LawFirmChangeEvent event) {
        this.lawFirmVo = event.getLawFirmVo();
        this.wxUserVo = event.getWxUserVo();
        createOrUpdateLawyer();
        createOrUpdateBrokerageUser();
    }

    /**
     * 创建或更新律师信息
     */
    private void createOrUpdateLawyer() {
        if (LawFirmScopeConstants.LAWYER.equals(lawFirmVo.getLawFirmType())) {
            //创建默认律师信息
            LawyerInfoVo lawyerInfoVo = lawyerInfoService.selectLawyerVoByWxUserId(wxUserVo.getWxUserId());
            if (lawyerInfoVo == null) {
                LawyerInfoBo lawyerInfoBo = new LawyerInfoBo();
                lawyerInfoBo.setLawFirmId(lawFirmVo.getLawFirmId());
                lawyerInfoBo.setLawyerName(lawFirmVo.getLawFirmName());
                lawyerInfoBo.setAvatar(lawFirmVo.getLawFirmLogo());
                lawyerInfoBo.setPracticeStartTime(lawFirmVo.getYearServiceTime());
                lawyerInfoBo.setWxUserId(wxUserVo.getWxUserId());
                lawyerInfoBo.setGroupType(LawFirmScopeConstants.LAWYER);
                if (lawFirmVo.getYearServiceTime() != null) {
                    long year = DateUtil.betweenYear(lawFirmVo.getYearServiceTime(), DateUtil.date(), true);
                    lawyerInfoBo.setPractice((int) year);
                }
                lawyerInfoBo.setAdviceStatus(lawFirmVo.getCurated());
                lawyerInfoBo.setFollowers(0);
                lawyerInfoBo.setConsultation(0);
                lawyerInfoBo.setLawyerDesc(lawFirmVo.getLawFirmDesc());
                lawyerInfoBo.setSpecial(lawFirmVo.getSpecial());
                lawyerInfoBo.setFeedback(lawFirmVo.getLawFirmTag());
                lawyerInfoBo.setProvince(lawFirmVo.getProvince());
                lawyerInfoBo.setCity(lawFirmVo.getCity());
                lawyerInfoBo.setDistrict(lawFirmVo.getDistrict());
                lawyerInfoService.insertByBo(lawyerInfoBo);
            } else {
                LawyerInfo lawyerInfo = MapstructUtils.convert(lawyerInfoVo, LawyerInfo.class);
                lawyerInfo.setLawyerName(lawFirmVo.getLawFirmName());
                lawyerInfo.setGroupType(LawFirmScopeConstants.LAWYER);
                lawyerInfo.setAvatar(lawFirmVo.getLawFirmLogo());
                lawyerInfo.setAdviceStatus(lawFirmVo.getCurated());
                lawyerInfo.setFeedback(lawFirmVo.getLawFirmTag());
                if (lawFirmVo.getYearServiceTime() != null) {
                    long year = DateUtil.betweenYear(lawFirmVo.getYearServiceTime(), DateUtil.date(), true);
                    lawyerInfo.setPractice((int) year);
                }
                lawyerInfo.setUpdateTime(DateUtil.date());
                lawyerInfo.setProvince(lawFirmVo.getProvince());
                lawyerInfo.setCity(lawFirmVo.getCity());
                lawyerInfo.setDistrict(lawFirmVo.getDistrict());
                if (LoginHelper.getUserId() != null) {
                    lawyerInfo.setUpdateBy(LoginHelper.getUserId());
                }
                lawyerInfoService.updateById(lawyerInfo);
            }
        }
    }

    /**
     * 创建或者更新分销信息
     */
    private void createOrUpdateBrokerageUser() {
        BrokerageUserVo brokerageUserVo = brokerageUserService.selectNullBrokerageUserVoByWxUserId(wxUserVo.getWxUserId());
        if (brokerageUserVo == null) {
            BrokerageUserBo brokerageUserBo = new BrokerageUserBo();
            brokerageUserBo.setWxUserId(wxUserVo.getWxUserId());
            brokerageUserBo.setNickname(wxUserVo.getNickname());
            brokerageUserBo.setAvatar(wxUserVo.getAvatar());
            brokerageUserBo.setBrokerageCode(IdWorkerUtil.nextIdStr());
            brokerageUserBo.setUserCount(0);
            brokerageUserBo.setOrderAmount(0L);
            brokerageUserBo.setOrderCount(0);
            brokerageUserBo.setWithdrawAmount(0L);
            brokerageUserBo.setWithdrawCount(0);
            brokerageUserService.insertByBo(brokerageUserBo);
        } else {
            brokerageUserVo.setNickname(wxUserVo.getNickname());
            brokerageUserVo.setAvatar(wxUserVo.getAvatar());
            BrokerageUser brokerageUser = MapstructUtils.convert(brokerageUserVo, BrokerageUser.class);
            brokerageUser.setUpdateTime(DateUtil.date());
            if (LoginHelper.getUserId() != null) {
                brokerageUser.setUpdateBy(LoginHelper.getUserId());
            }
            brokerageUserService.updateById(brokerageUser);
        }
    }
}
